// ------------------------------------
// Reducer
// ------------------------------------
import { createReducer, statusToError, getStatusError } from 'http-services'
import * as types from './constant'
import * as storageService from 'services/storage'
import { setToken } from 'services/token'

const initState = {
  accessTokenPending: false,
  accessTokenError: -1,
  accessTokenMessage: null,
  loginPending: false,
  loginError: -1,
  loginMessage: null,
  auth: null,
  rules: null,
  teams: null,
  rtsp_keys: null,
  rtsp_key: undefined,
  logoutPending: false,
  getHomePending: false,
  getHomeError: -1,
  getHomeMessage: null,
  homeData: null,
  //
  retrievekeyPending: false,
  retrievekeyError: -1,
  retrievekeyMessage: null,
  editInfoPending: false,
  editInfoError: -1,
  editInfoMessage: null,
  //
}

const ACTION_HANDLERS = {
  [types.PASSPORT_FETCH_ACCESSTOKEN_BEGIN]: (state, action) => {
    return {
      ...state,
      accessTokenPending: true,
      accessTokenError: -1,
      accessTokenMessage: null,
      /*auth: null,
      rules: null,
      teams: null,
      rtsp_keys: null*/
    }
  },
  [types.PASSPORT_FETCH_ACCESSTOKEN_SUCCESS]: (state, action) => {
    const { payload } = action
    const { data, Status } = payload
    let newState = null
    if (data && Status.code === 0) {
      let { auth, rules, teams, rtsp_keys } = data
      newState = { auth, rules, teams, rtsp_keys }
      if (rtsp_keys.length > 0) {
        newState = { ...newState, rtsp_key: rtsp_keys[0] }
      }
    }
    return {
      ...state,
      ...statusToError(payload, 'accessTokenError', 'accessTokenMessage'),
      accessTokenPending: false,
      ...newState
    }
  },
  [types.PASSPORT_FETCH_ACCESSTOKEN_FAILURE]: (state, action) => {
    const { error } = action
    const status = getStatusError(error)
    return {
      ...state,
      ...statusToError({ status }, 'accessTokenError', 'accessTokenMessage'),
      accessTokenPending: false,
    }
  },
  [types.PASSPORT_FETCH_HOME_BEGIN]: (state, action) => {
    return {
      ...state,
      getHomePending: true,
      getHomeError: -1,
      getHomeMessage: null,
      homeData: null
    }
  },
  [types.PASSPORT_FETCH_HOME_SUCCESS]: (state, action) => {
    const { payload } = action
    const { data, Status } = payload
    let newState = null
    if (data && Status.code === 0) {
      newState = { homeData: data }
    }
    return {
      ...state,
      ...statusToError(payload, 'getHomeError', 'getHomeMessage'),
      getHomePending: false,
      ...newState
    }
  },
  [types.PASSPORT_FETCH_HOME_FAILURE]: (state, action) => {
    const { error } = action
    const status = getStatusError(error)
    return {
      ...state,
      ...statusToError({ status }, 'getHomeError', 'getHomeMessage'),
      getHomePending: false,
    }
  },
  [types.PASSPORT_FETCH_LOGIN_BEGIN]: (state, action) => {
    return {
      ...state,
      loginPending: true,
      loginError: -1,
      loginMessage: null,
      auth: null,
      rules: null,
      teams: null,
      rtsp_keys: null
    }
  },
  [types.PASSPORT_FETCH_LOGIN_SUCCESS]: (state, action) => {
    const { payload } = action
    const { data, Status } = payload
    let newState = null
    if (data && Status.code === 0) {
      let { auth, rules, teams, rtsp_keys } = data
      newState = { auth, rules, teams, rtsp_keys }
      if (rtsp_keys.length > 0) {
        newState = { ...newState, rtsp_key: rtsp_keys[0] }
      }
      updateAuth(payload)
    }
    return {
      ...state,
      ...statusToError(payload, 'loginError', 'loginMessage'),
      loginPending: false,
      ...newState
    }
  },
  [types.PASSPORT_FETCH_LOGIN_FAILURE]: (state, action) => {
    const { error } = action
    const status = getStatusError(error)
    return {
      ...state,
      ...statusToError({ status }, 'loginError', 'loginMessage'),
      loginPending: false,
    }
  },
  [types.PASSPORT_LOCAL_LOGOUT_BEGIN]: (state, action) => {
    return {
      ...state,
      logoutPending: true
    }
  },
  [types.PASSPORT_LOCAL_LOGOUT_FINISH]: (state, action) => {
    updateAuth(null)
    return {
      ...state,
      logoutPending: false,
      auth: null,
      rules: null,
      teams: null,
      rtsp_keys: null,
      rtsp_key: undefined
    }
  },
  //
  [types.PASSPORT_FETCH_RETRIEVEKEY_BEGIN]: (state, action) => {
    return {
      ...state,
      retrievekeyPending: true,
      retrievekeyError: -1,
      retrievekeyMessage: null,
    }
  },
  [types.PASSPORT_FETCH_RETRIEVEKEY_SUCCESS]: (state, action) => {
    const { payload } = action
    return {
      ...state,
      ...statusToError(payload, 'retrievekeyError', 'retrievekeyMessage'),
      retrievekeyPending: false,
    }
  },
  [types.PASSPORT_FETCH_RETRIEVEKEY_FAILURE]: (state, action) => {
    const { error } = action
    const status = getStatusError(error)
    return {
      ...state,
      ...statusToError({ status }, 'retrievekeyError', 'retrievekeyMessage'),
      retrievekeyPending: false,
    }
  },
  [types.PASSPORT_FETCH_EDITINFO_BEGIN]: (state, action) => {
    return {
      ...state,
      editInfoPending: true,
      editInfoError: -1,
      editInfoMessage: null,
    }
  },
  [types.PASSPORT_FETCH_EDITINFO_SUCCESS]: (state, action) => {
    const { payload } = action
    const { data, Status } = payload
    return {
      ...state,
      ...statusToError(payload, 'editInfoError', 'editInfoMessage'),
      editInfoPending: false,
      auth: { ...state.auth, ...data }
    }
  },
  [types.PASSPORT_FETCH_EDITINFO_FAILURE]: (state, action) => {
    const { error } = action
    const status = getStatusError(error)
    return {
      ...state,
      ...statusToError({ status }, 'editInfoError', 'editInfoMessage'),
      editInfoPending: false,
    }
  },
  //
  [types.PASSPORT_LOCAL_UPDATE_AUTHINFO]: (state, action) => {
    const { payload } = action
    return {
      ...state,
      auth: { ...state.auth, ...payload.info }
    }
  },
  [types.PASSPORT_LOCAL_SETRUTP_KEY]: (state, action) => {
    const { payload } = action
    return {
      ...state,
      rtsp_key: payload.key === state.rtsp_keys[0] ? undefined : payload.key
    }
  }
}

export default (state = initState, action) => createReducer(state, action, ACTION_HANDLERS)

function updateAuth (payload) {
  if (!payload) {
    storageService.removeItem('auth')
    setToken(null)
    return
  }
  const { data, Status } = payload
  if (Status.code === 0) {
    const { auth } = data
    storageService.setItem('auth', auth)
    setToken(auth.accesskey)
  }
}